package com.example.ymm.modules1.service.impl.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.*;
import com.example.ymm.modules1.mapper.my.user.DbDeptUserMapper;
import com.example.ymm.modules1.service.dto.DbDeptUserQueryCriteria;
import com.example.ymm.modules1.service.user.DbDeptService;
import com.example.ymm.modules1.service.user.DbDeptUserService;
import com.example.ymm.modules1.service.user.DbUserService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class DbDeptUserServiceImpl extends BaseServiceImpl<DbDeptUserMapper, DbDeptUser> implements DbDeptUserService {

    @Autowired
    DbUserService dbUserService;

    @Autowired
    DbDeptService dbDeptService;

    @Override
    public TableList<ViewDeptUser> getDeptUserList(DbDeptUserQueryCriteria criteria, YmPageDto pageDto) {
        getPage(pageDto);
        IPage<ViewDeptUser> page = new Page<ViewDeptUser>(pageDto.getPage(),pageDto.getPageSize());
        List<ViewDeptUser> maps = getDeptUserList(criteria);
        PageInfo<ViewDeptUser> pageInfo = new PageInfo<>(maps);
        TableList<ViewDeptUser> tableList=new TableList<>(pageInfo);
        return tableList;
    }

    public List<ViewDeptUser> getDeptUserList(DbDeptUserQueryCriteria criteria) {
        return baseMapper.getDeptUserList(criteria);
    }

    @Override
    public void removeByUserId(String userId) {
        LambdaQueryWrapper<DbDeptUser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DbDeptUser::getUserId,userId);
        remove(queryWrapper);
    }

    @Override
    public void removeByAll() {
        LambdaQueryWrapper<DbDeptUser> queryWrapper=new LambdaQueryWrapper<>();
        remove(queryWrapper);
    }

    @Override
    public void syncByJson(JSONObject json, DbDept dept) {
        JSONArray list = json.getJSONArray("list");
      //  System.out.println("---list:"+list);
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = list.getJSONObject(i);
            DbUser dbUser = dbUserService.addUser(jsonObject);
           // removeByUserId(dbUser.getUserId());
            DbDeptUser dbDeptUser=new DbDeptUser();
            dbDeptUser.setDeptId(dept.getDeptId());
            dbDeptUser.setUserId(dbUser.getUserId());
            saveOrUpdate(dbDeptUser);

            //新员工入群

        }
    }

    @Override
    public List<ViewDeptUser> findByPath(String path) {
        String[] split = path.split("/");

        List<DbDept> deptTreeList = dbDeptService.getDeptTreeList(null);
        Integer index=0;
        String name = split[index];
        DbDept dbDept=null;
        for(DbDept dept:deptTreeList){
            if(name.equals(dept.getName())){
                if(UtilValidate.isNotEmpty(dept.getChildren())){
                    index=index+1;
                    dbDept=forEachFindPath(dept.getChildren(), split, index);

                }else{
                    dbDept=dept;
                }
            }
        }
//        DbDept dbDept = forEachFindPath(deptTreeList, split, index);
//        System.out.println("--dbDept::"+dbDept);
        List<ViewDeptUser> deptUserList=new ArrayList<>();
        if(UtilValidate.isNotEmpty(dbDept)){
            DbDeptUserQueryCriteria criteria=new DbDeptUserQueryCriteria();
            criteria.setDeptId(dbDept.getDeptId());
            deptUserList = baseMapper.getDeptUserList(criteria);
        }

        return deptUserList;
    }

    public DbDept forEachFindPath(List<DbDept> deptTreeList,String[] split,Integer index){
        String name = split[index];
        DbDept result=null;
        for(DbDept dept:deptTreeList){
            if(name.equals(dept.getName())){
                if(UtilValidate.isNotEmpty(dept.getChildren())){
                    index=index+1;
                    result=forEachFindPath(dept.getChildren(), split, index);

                }else{
                    result=dept;
                }
            }
        }
        return result;
    }


    @Override
    public List<String> findUserNameByPath(String path) {
        List<ViewDeptUser> byPath = findByPath(path);
        List<String> arrs=new ArrayList<>();
        for(ViewDeptUser user:byPath){
            arrs.add(user.getUserName());
        }
        System.out.println(path+"::"+String.join(",",arrs));
        return arrs;
    }

    @Override
    public List<DbDeptUser> queryAll(DbDeptUserQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbDeptUser.class, criteria);
        List<DbDeptUser> result =baseMapper.selectList(predicate);
        return result;
    }
}
